package com.atkinsglobal.basic;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.CharBuffer;
import java.util.Locale;

import org.springframework.context.ApplicationContext;
//import org.springframework.context.annotation.AnnotationConfigApplicationContext;
//import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

//import com.atkinsglobal.basic.cfg.AppConfig;
import com.atkinsglobal.basic.cfg.BigBean;
import com.atkinsglobal.basic.resource.BeanWithRes;

public class Play {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		//ApplicationContext ctx = new ClassPathXmlApplicationContext("console.xml");
		ApplicationContext ctx = new FileSystemXmlApplicationContext("console.xml");
		
		Person jx = ctx.getBean("John", Person.class);
		
		System.out.println(jx.getName());
		
		ClientService s = ctx.getBean("clientService", ClientService.class);
		
		System.out.println(s.name);
		
		ClientServiceConsumer consumer = ctx.getBean("serviceConsumer", ClientServiceConsumer.class);
		
		System.out.println(consumer.getBoundService().name);
		System.out.println(consumer.getBoundService().getVersion().getVersionNo());
		
		System.out.println(consumer.getSomeMap().get("A"));
		
		ClientServiceConsumer consumer2 = ctx.getBean("serviceConsumerChild", ClientServiceConsumer.class);
		System.out.println(consumer2.getSomeMap().get("D"));
		
		System.out.println(consumer2.getAutoWireVersion().getVersionNo());
		
		System.out.println("Context Aware");
		CtxAware aware = ctx.getBean("awareGeek", CtxAware.class);
		Person p = aware.getMeSomeone();
		System.out.println(p.getName());
		
		// Set the singleton Bob
		// Change scope of bean id="Px1" to see the difference
		p.setName("Bob");
		System.out.println("jx's name: " + jx.getName());
		
		//AbstractApplicationContext abstractCtx = new FileSystemXmlApplicationContext("console.xml");
		//abstractCtx.registerShutdownHook();
		
		// Get autowired project.. Each phase is qualified by the custom PhaseQualifier
		System.out.println("#### Autowired project:");
		Project proj = ctx.getBean("project1", Project.class);
		System.out.println(proj.getName());
		System.out.println(proj.getDesignPhase().getPahseName() + " : " + proj.getDesignPhase().getManager());
		System.out.println(proj.getImplementationPhase().getPahseName() + " : " + proj.getImplementationPhase().getManager());
		
		
		System.out.println("#### Pure annotation spec");
//		ApplicationContext ctxAnnotated = new AnnotationConfigApplicationContext(AppConfig.class);
		// Not required to get new context as the framework auto detects it when scanning components
		BigBean bean1 = ctx.getBean(BigBean.class);
		System.out.println(bean1.getName() + " : " + bean1.getNumber());
		
		System.out.println("#### Messages");
		System.out.println(ctx.getMessage("argument.required", new Object[] {new String("My poon")}, "Default", Locale.ENGLISH) );
	
	
		//Resources
		System.out.println("#### Resources");
		BeanWithRes resBean = ctx.getBean("resBean", BeanWithRes.class);
		
		try {
			InputStreamReader r = new InputStreamReader(resBean.getTemplate().getInputStream());
			char[] buff = new char[10];
			r.read(buff);
			System.out.println(buff);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("#### Validation");
		
	}

}
